package service

import (
	"ginblog/enum"
	"ginblog/model"
	"ginblog/model/vo"
)

type ArticleService struct {
	user UserService
}

type ArticleSerInterface interface {
	CheckUser(name string) enum.Code
	CreateArticle(data *model.Article, userName string) enum.Code
	GetArticles(title string, pageSize, pageNum int) ([]vo.ArticleVo, int64)
	GetArticleById(articleId int) (model.Article, string)
	EditArticle(id int, data *model.Article) enum.Code
	DeleteArticle(id int) enum.Code
	GetCateArt(cateId int, pageSize int, pageNum int) ([]vo.ArticleVo, int64)
}

// CheckUser 查询用户名是否被使用
func (article *ArticleService) CheckUser(name string) enum.Code {
	var data model.User
	db.Select("id").Where("username = ?", name).First(&data)
	if data.ID > 0 {
		return enum.ErrorUsernameUsed
	}
	return enum.SUCCESS
}

// CreateArticle 添加文章
func (article *ArticleService) CreateArticle(data *model.Article, userName string) enum.Code {
	var user model.User
	// 获取用户的id
	db.Select("id").Where("username = ?", userName).First(&user)

	data.Uid = user.ID

	err := db.Create(data).Error
	if err != nil {
		return enum.ERROR
	}
	return enum.SUCCESS
}

// GetArticles 查询文章列表
func (article *ArticleService) GetArticles(title string, pageSize, pageNum int) ([]vo.ArticleVo, int64) {
	var artic []model.Article
	var count int64 = 0
	var articlevo []vo.ArticleVo

	// 搜索文章
	if title != "" {
		// 单独计数
		db.Model(&artic).Where("title like ?", "%"+title+"%").Count(&count)
		err := db.Model(&artic).Where("title like ?", "%"+title+"%").Order("article.created_at DESC").Select("article.id art_id,article.created_at art_created,article.updated_at art_updated,article.title art_title ,article.desc art_desc,article.content art_content,article.img art_img,article.uid art_uid,article.read_count read_count,user.username username,category.name cate_name").Joins("left join category on article.cid = category.id left join user on  article.uid = user.id").Limit(pageSize).Offset((pageNum - 1) * pageSize).Scan(&articlevo).Error
		//err := db.Preload("Category").Where("title like ?", "%"+title+"%").Limit(pageSize).Offset((pageNum - 1) * pageSize).Find(&artic).Count(&count).Error
		if err != nil {

			return nil, 0
		}
		return articlevo, count
	}
	// 单独计数
	db.Model(&artic).Count(&count)
	// 获取文章列表
	err := db.Model(&artic).Order("article.created_at DESC").Select("article.id art_id,article.created_at art_created,article.updated_at art_updated,article.title art_title ,article.desc art_desc,article.content art_content,article.img art_img, article.uid art_uid,article.read_count read_count,user.username username,category.name cate_name").Joins("left join category on article.cid = category.id left join user on  article.uid = user.id").Limit(pageSize).Offset((pageNum - 1) * pageSize).Scan(&articlevo).Error
	if err != nil {
		return nil, 0
	}
	return articlevo, count
}

// GetArticleById 根据id查询文章
func (article *ArticleService) GetArticleById(articleId int) (model.Article, string) {
	var artic model.Article
	if articleId == 0 {
		return artic, ""
	}
	db.Preload("Category").First(&artic, articleId)
	uid := int(artic.Uid)

	// 获取用户信息
	user := article.user.GetUserById(uid)

	// 新增查看人数
	if user.Username != "" {
		db.Model(&artic).Updates(map[string]interface{}{"read_count": artic.ReadCount + 1})
	}

	return artic, user.Username
}

// EditArticle 编辑文章
func (article *ArticleService) EditArticle(id int, data *model.Article) enum.Code {

	// 编辑文章
	maps := make(map[string]interface{})
	maps["Title"] = data.Title
	maps["Cid"] = data.Cid
	maps["Desc"] = data.Desc
	maps["Content"] = data.Content
	maps["Img"] = data.Img

	err := db.Model(&model.Article{}).Where("id = ? and deleted_at is null", id).Updates(maps).Error
	if err != nil {
		return enum.ERROR
	}
	return enum.SUCCESS
}

// DeleteArticle 删除文章
func (article *ArticleService) DeleteArticle(id int) enum.Code {
	artic := model.Article{}

	// 删除用户，软删除
	err := db.Where("id = ?", id).Delete(&artic).Error
	if err != nil {
		return enum.ERROR
	}
	return enum.SUCCESS
}

// GetCateArt 查询指定分类下的文章列表
func (article *ArticleService) GetCateArt(cateId int, pageSize int, pageNum int) ([]vo.ArticleVo, int64) {
	var artic []model.Article
	var count int64 = 0
	var articlevo []vo.ArticleVo
	// 获取文章列表
	//err := db.Preload("Category").Where("cid = ?", cateId).Limit(pageSize).Offset((pageNum - 1) * pageSize).Find(&artic).Count(&count).Error
	err := db.Model(&artic).Where("cid = ?", cateId).Order("article.created_at DESC").Select("article.id art_id,article.created_at art_created,article.updated_at art_updated,article.title art_title ,article.desc art_desc,article.content art_content,article.img art_img,article.uid art_uid,article.read_count read_count,user.username username,category.name cate_name").Joins("left join category on article.cid = category.id left join user on  article.uid = user.id").Limit(pageSize).Offset((pageNum - 1) * pageSize).Scan(&articlevo).Count(&count).Error
	if err != nil {
		return nil, 0
	}
	return articlevo, count
}
